<?php
    session_start();

    /*
    Disconnect current user
    */
	function logout()
	{

		// open the user file
		if($ConnexionList = file('../data/User'))
		{
			if(isset($_SESSION['pseudo']))
			{
				// selected the index of the current user
				$i=recherchePseudo($_SESSION['pseudo'],$ConnexionList);
				if($i)
				{
					// delete him in the user file 
					unset($ConnexionList[$i]);
					if(file_put_contents("../data/User", implode($ConnexionList)))
					{
						// destroy the current session
						session_destroy(); 
						// login ok, redirect to the chat
						if(isset($_GET['lang']))
						{
							header('Location: ../index.php?lang='.$_GET['lang']);
						}
						else
						{
							header('Location: ../index.php?lang=fr'); 
						}
						  
					}
					else
					{
						echo "erreur écriture";
					}
				}
				else
				{
					echo 'erreur pseudo i= '.$i;
				}
			}
			else
			{
				echo "erreur var session";
			}
		}
		else
		{
			echo "erreur ouverture fichier";
		}
	}
	



    /*
    Disconnect an particular user, this function permit to logout automaticaly an inactive user
    */
	function logoutPseudo($pseudo)
	{

		if($ConnexionList = file('../data/User'))
		{
			$i=recherchePseudo($pseudo,$ConnexionList);
			if($i)
			{
				unset($ConnexionList[$i]);
				if(file_put_contents("../data/User", implode($ConnexionList)))
				{
					// login ok, redirect to the chat   
				}
				else
				{
					echo "erreur écriture";
				}
			}
			else
			{
				echo 'erreur pseudo i= '.$i;
			}

		}
		else
		{
			echo "erreur ouverture fichier";
		}
	}

	?>
